package org.flowable.engine.impl.util.condition;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jnpf.engine.custom.handler.condition.NashornCondition;
import jnpf.engine.model.flowengine.shuntjson.childnode.Properties;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.engine.DynamicBpmnConstants;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.Condition;
import org.flowable.engine.impl.context.BpmnOverrideContext;
import org.flowable.engine.impl.context.Context;
import org.flowable.engine.impl.el.UelExpressionCondition;
import org.flowable.engine.impl.util.CommandContextUtil;

import static jnpf.engine.custom.constants.BasicActivitiConstants.exActivitiPrefixURL;
import static jnpf.engine.custom.constants.BasicActivitiConstants.extensionAttributeName;
import static jnpf.engine.util.BpmnModelUtils.getAttributeValueByTaskDefinitionKey;
import static jnpf.engine.util.BpmnModelUtils.getBpmnModelMainProcess;

/*
*
 * @author LoveMyOrange
 * @create 2022-02-05 23:07

*/

public class ConditionUtil {
    public static boolean hasTrueCondition(SequenceFlow sequenceFlow, DelegateExecution execution) {
        String conditionExpression = null;
        if (Context.getProcessEngineConfiguration().isEnableProcessDefinitionInfoCache()) {
            ObjectNode elementProperties = BpmnOverrideContext.getBpmnOverrideElementProperties(sequenceFlow.getId(), execution.getProcessDefinitionId());
            conditionExpression = getActiveValue(sequenceFlow.getConditionExpression(), DynamicBpmnConstants.SEQUENCE_FLOW_CONDITION, elementProperties);
        } else {
            conditionExpression = sequenceFlow.getConditionExpression();
        }

        if (StringUtils.isNotEmpty(conditionExpression)) {
            String processDefinitionId = execution.getProcessDefinitionId();
            Process bpmnModelMainProcess = getBpmnModelMainProcess(processDefinitionId);
            String sequenceFlowStr = getAttributeValueByTaskDefinitionKey(bpmnModelMainProcess, sequenceFlow.getId(), "sequenceFlow");
            Properties properties = JSONObject.parseObject(sequenceFlowStr, new TypeReference<Properties>() {
            });

            Integer modeFlag = properties.getModeFlag();
            Condition condition =null;
            //Vue3版本的流程图
            if(null==modeFlag){
                condition = new NashornCondition(sequenceFlowStr);
            }
            else{
                Expression expression = CommandContextUtil.getProcessEngineConfiguration().getExpressionManager().createExpression(conditionExpression);
                condition=new UelExpressionCondition(expression);
            }
            if (condition.evaluate(sequenceFlow.getId(), execution)) {
                return true;
            }
            return false;
        } else {
            return true;
        }

    }

    protected static String getActiveValue(String originalValue, String propertyName, ObjectNode elementProperties) {
        String activeValue = originalValue;
        if (elementProperties != null) {
            JsonNode overrideValueNode = elementProperties.get(propertyName);
            if (overrideValueNode != null) {
                if (overrideValueNode.isNull()) {
                    activeValue = null;
                } else {
                    activeValue = overrideValueNode.asText();
                }
            }
        }
        return activeValue;
    }

}
